#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* longestCommonPrefix(char** strs, int strsSize) {
    if (strsSize == 0) {
        char* result = (char*)malloc(1);
        if (result == NULL) {
            return NULL;
        }
    }
    char* s0 = strs[0];
    int len = strlen(s0);
    for (int j = 0; s0[j]; j++) {
        for (int i = 1; i < strsSize; ++i) {
            if (s0[j] != strs[i][j] || strs[i][j] == 0) {
                char* result = (char*)malloc(sizeof(char) * (j + 1));
                if (result == NULL) {
                    return NULL;
                }
                strncpy(result, s0, j);
                result[j] = '\0';
                return result;
            }
        }
    }
    char* result = (char*)malloc(sizeof(char) * (len + 1));
    if (result == NULL) {
        return NULL;
    }
    strcpy(result, s0);
    result[len] = '\0';
    return result;
}
int main() {
    char* strs[] = { "flower", "flow", "flight" };
    int strsSize = sizeof(strs) / sizeof(strs[0]);
    char* prefix = longestCommonPrefix(strs, strsSize);
    if (prefix != NULL) {
        printf("Longest common prefix: %s\n", prefix);
        free(prefix);
    }
    return 0;
}
